import requests

from simpleutil.web import parse_curl
kb_id = 1178

post_curl="""
curl 'https://kms.datagrand.com/api/v1/knowledge_item/knowledge_details' \
  -H 'authority: kms.datagrand.com' \
  -H 'accept: application/json, text/plain, */*' \
  -H 'accept-language: zh-CN,zh;q=0.9' \
  -H 'appauth: MTk5ODc1X3dpa2k=' \
  -H 'authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3MzQ5MjA5ODksIm5iZiI6MTczNDkyMDk4OSwianRpIjoiODQ5ODcwY2MtN2E2OS00MzhjLWE5ZmQtZjc5NDMxMDQ4NDJkIiwiZXhwIjoxNzM1NTI1Nzg5LCJpZGVudGl0eSI6WyJVc2VyXzE3MzE4OTY3OTAiLG51bGxdLCJmcmVzaCI6ZmFsc2UsInR5cGUiOiJhY2Nlc3MiLCJ1c2VyX2NsYWltcyI6eyJ1c2VyaWQiOjM3MzMsInVzZXJuYW1lIjoiVXNlcl8xNzMxODk2NzkwIiwibGV2ZWwiOiJcdTY2NmVcdTkwMWFcdTc1MjhcdTYyMzciLCJ1c2VyX25pY2tuYW1lIjoiXHU2NzY4XHU3Mzg5XHU3NDJhIiwidXNlcl9pY29uIjoiLi9hc3NldHMvaW1hZ2VzL2F2YXRhci5zdmciLCJ1c2VyX2NlbnRlcl9pZCI6bnVsbH19.k-DRiUebRanN5TRxYJcWr0fPF2qcalzLCnKN_9rP2sY' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundaryh1d01xaJBZecxnN6' \
  -H 'cookie: service_type=km; _file_auth_info=eyJBdXRob3JpemF0aW9uIjoiQmVhcmVyIGV5SjBlWEFpT2lKS1YxUWlMQ0poYkdjaU9pSklVekkxTmlKOS5leUpwWVhRaU9qRTNNelE1TWpBNU9Ea3NJbTVpWmlJNk1UY3pORGt5TURrNE9Td2lhblJwSWpvaU9EUTVPRGN3WTJNdE4yRTJPUzAwTXpoakxXRTVabVF0WmpjNU5ETXhNRFE0TkRKa0lpd2laWGh3SWpveE56TTFOVEkxTnpnNUxDSnBaR1Z1ZEdsMGVTSTZXeUpWYzJWeVh6RTNNekU0T1RZM09UQWlMRzUxYkd4ZExDSm1jbVZ6YUNJNlptRnNjMlVzSW5SNWNHVWlPaUpoWTJObGMzTWlMQ0oxYzJWeVgyTnNZV2x0Y3lJNmV5SjFjMlZ5YVdRaU9qTTNNek1zSW5WelpYSnVZVzFsSWpvaVZYTmxjbDh4TnpNeE9EazJOemt3SWl3aWJHVjJaV3dpT2lKY2RUWTJObVZjZFRrd01XRmNkVGMxTWpoY2RUWXlNemNpTENKMWMyVnlYMjVwWTJ0dVlXMWxJam9pWEhVMk56WTRYSFUzTXpnNVhIVTNOREpoSWl3aWRYTmxjbDlwWTI5dUlqb2lMaTloYzNObGRITXZhVzFoWjJWekwyRjJZWFJoY2k1emRtY2lMQ0oxYzJWeVgyTmxiblJsY2w5cFpDSTZiblZzYkgxOS5rLURSaVVlYlJhbk41VFJ4WUpjV3IwZlBGMnFjYWx6TENuS05fOXJQMnNZIiwiQXBwYXV0aCI6Ik1UazVPRGMxWDNkcGEyaz0iLCJYLUFwcC1JZCI6MTk5ODc1LCJYLVNlcnZpY2UtVHlwZSI6ImttIiwiWC1BcHAtTmFtZSI6Indpa2kifQ==' \
  -H 'kms-tag: kms' \
  -H 'origin: https://kms.datagrand.com' \
  -H 'referer: https://kms.datagrand.com/fronted/' \
  -H 'sec-ch-ua: "Not:A-Brand";v="99", "Chromium";v="112"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "macOS"' \
  -H 'sec-fetch-dest: empty' \
  -H 'sec-fetch-mode: cors' \
  -H 'sec-fetch-site: same-origin' \
  -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 QuarkPC/1.10.5.187' \
  -H 'x-app-id: 199875' \
  -H 'x-app-name: wiki' \
  -H 'x-service-type: km' \
  --data-raw $'------WebKitFormBoundaryh1d01xaJBZecxnN6\r\nContent-Disposition: form-data; name="kb_id"\r\n\r\n1178\r\n------WebKitFormBoundaryh1d01xaJBZecxnN6\r\nContent-Disposition: form-data; name="cate_parent_id"\r\n\r\n75109\r\n------WebKitFormBoundaryh1d01xaJBZecxnN6\r\nContent-Disposition: form-data; name="dir_type"\r\n\r\n0\r\n------WebKitFormBoundaryh1d01xaJBZecxnN6--\r\n' \
  --compressed
"""

put_curl="""
curl 'https://kms.datagrand.com/api/v1/knowledge_item/knowledge_details' \
  -X 'PUT' \
  -H 'authority: kms.datagrand.com' \
  -H 'accept: application/json, text/plain, */*' \
  -H 'accept-language: zh-CN,zh;q=0.9' \
  -H 'appauth: MTk5ODc1X3dpa2k=' \
  -H 'authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3MzQ5MjA5ODksIm5iZiI6MTczNDkyMDk4OSwianRpIjoiODQ5ODcwY2MtN2E2OS00MzhjLWE5ZmQtZjc5NDMxMDQ4NDJkIiwiZXhwIjoxNzM1NTI1Nzg5LCJpZGVudGl0eSI6WyJVc2VyXzE3MzE4OTY3OTAiLG51bGxdLCJmcmVzaCI6ZmFsc2UsInR5cGUiOiJhY2Nlc3MiLCJ1c2VyX2NsYWltcyI6eyJ1c2VyaWQiOjM3MzMsInVzZXJuYW1lIjoiVXNlcl8xNzMxODk2NzkwIiwibGV2ZWwiOiJcdTY2NmVcdTkwMWFcdTc1MjhcdTYyMzciLCJ1c2VyX25pY2tuYW1lIjoiXHU2NzY4XHU3Mzg5XHU3NDJhIiwidXNlcl9pY29uIjoiLi9hc3NldHMvaW1hZ2VzL2F2YXRhci5zdmciLCJ1c2VyX2NlbnRlcl9pZCI6bnVsbH19.k-DRiUebRanN5TRxYJcWr0fPF2qcalzLCnKN_9rP2sY' \
  -H 'content-type: application/json' \
  -H 'cookie: service_type=km; _file_auth_info=eyJBdXRob3JpemF0aW9uIjoiQmVhcmVyIGV5SjBlWEFpT2lKS1YxUWlMQ0poYkdjaU9pSklVekkxTmlKOS5leUpwWVhRaU9qRTNNelE1TWpBNU9Ea3NJbTVpWmlJNk1UY3pORGt5TURrNE9Td2lhblJwSWpvaU9EUTVPRGN3WTJNdE4yRTJPUzAwTXpoakxXRTVabVF0WmpjNU5ETXhNRFE0TkRKa0lpd2laWGh3SWpveE56TTFOVEkxTnpnNUxDSnBaR1Z1ZEdsMGVTSTZXeUpWYzJWeVh6RTNNekU0T1RZM09UQWlMRzUxYkd4ZExDSm1jbVZ6YUNJNlptRnNjMlVzSW5SNWNHVWlPaUpoWTJObGMzTWlMQ0oxYzJWeVgyTnNZV2x0Y3lJNmV5SjFjMlZ5YVdRaU9qTTNNek1zSW5WelpYSnVZVzFsSWpvaVZYTmxjbDh4TnpNeE9EazJOemt3SWl3aWJHVjJaV3dpT2lKY2RUWTJObVZjZFRrd01XRmNkVGMxTWpoY2RUWXlNemNpTENKMWMyVnlYMjVwWTJ0dVlXMWxJam9pWEhVMk56WTRYSFUzTXpnNVhIVTNOREpoSWl3aWRYTmxjbDlwWTI5dUlqb2lMaTloYzNObGRITXZhVzFoWjJWekwyRjJZWFJoY2k1emRtY2lMQ0oxYzJWeVgyTmxiblJsY2w5cFpDSTZiblZzYkgxOS5rLURSaVVlYlJhbk41VFJ4WUpjV3IwZlBGMnFjYWx6TENuS05fOXJQMnNZIiwiQXBwYXV0aCI6Ik1UazVPRGMxWDNkcGEyaz0iLCJYLUFwcC1JZCI6MTk5ODc1LCJYLVNlcnZpY2UtVHlwZSI6ImttIiwiWC1BcHAtTmFtZSI6Indpa2kifQ==' \
  -H 'kms-tag: kms' \
  -H 'origin: https://kms.datagrand.com' \
  -H 'referer: https://kms.datagrand.com/fronted/' \
  -H 'sec-ch-ua: "Not:A-Brand";v="99", "Chromium";v="112"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "macOS"' \
  -H 'sec-fetch-dest: empty' \
  -H 'sec-fetch-mode: cors' \
  -H 'sec-fetch-site: same-origin' \
  -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 QuarkPC/1.10.5.187' \
  -H 'x-app-id: 199875' \
  -H 'x-app-name: wiki' \
  -H 'x-service-type: km' \
  --data-raw '{"knowledge_id":75498,"name":"模版","author":"User_1731896790","content":"[{\"id\":\"RElt8\",\"type\":\"p\",\"children\":[{\"text\":\"\"}]},{\"children\":[{\"children\":[{\"background\":\"rgb(1, 74, 150)\",\"type\":\"td\",\"children\":[{\"children\":[{\"text\":\"所属公司\",\"fontSize\":\"11pt\",\"color\":\"white\"}],\"type\":\"p\",\"id\":\"r7twa\"}],\"id\":\"kpyf6\"},{\"type\":\"td\",\"children\":[{\"children\":[{\"text\":\"勘察设研公司\",\"fontSize\":\"11pt\"}],\"type\":\"p\",\"id\":\"az0cr\"}],\"id\":\"9quhg\"}],\"type\":\"tr\",\"id\":\"eucam\"},{\"children\":[{\"background\":\"rgb(1, 74, 150)\",\"type\":\"td\",\"children\":[{\"children\":[{\"text\":\"职务\",\"fontSize\":\"11pt\",\"color\":\"white\"}],\"type\":\"p\",\"id\":\"urj3m\"}],\"id\":\"n9g9d\"},{\"type\":\"td\",\"children\":[{\"children\":[{\"text\":\"执行所长\",\"fontSize\":\"11pt\"}],\"type\":\"p\",\"id\":\"meyyn\"}],\"id\":\"4tdgw\"}],\"type\":\"tr\",\"id\":\"v1ybw\"},{\"children\":[{\"background\":\"rgb(1, 74, 150)\",\"type\":\"td\",\"children\":[{\"children\":[{\"text\":\"职称\",\"fontSize\":\"11pt\",\"color\":\"white\"}],\"type\":\"p\",\"id\":\"dogmm\"}],\"id\":\"9dyp7\"},{\"type\":\"td\",\"children\":[{\"children\":[{\"text\":\"正高级工程师\",\"fontSize\":\"11pt\"}],\"type\":\"p\",\"id\":\"ba6v5\"}],\"id\":\"60tbb\"}],\"type\":\"tr\",\"id\":\"u5w35\"}],\"type\":\"table\",\"id\":\"ztpty\"},{\"children\":[{\"text\":\"\"}],\"type\":\"p\",\"id\":\"e5x5z\"},{\"children\":[{\"children\":[{\"background\":\"rgb(1, 74, 150)\",\"type\":\"td\",\"attributes\":{\"rowspan\":\"4\"},\"children\":[{\"children\":[{\"text\":\"职业资格证书\",\"fontSize\":\"11pt\",\"color\":\"white\"}],\"type\":\"p\",\"id\":\"brs8x\"}],\"rowSpan\":3,\"id\":\"ta5ov\"},{\"type\":\"td\",\"children\":[{\"children\":[{\"text\":\"注册土木工程师（港航）\",\"fontSize\":\"11pt\",\"color\":\"windowtext\"}],\"type\":\"p\",\"id\":\"f5sfj\"}],\"id\":\"j3uwn\"}],\"type\":\"tr\",\"id\":\"xqcrx\"},{\"children\":[{\"type\":\"td\",\"children\":[{\"children\":[{\"text\":\"\",\"fontSize\":\"11pt\",\"color\":\"windowtext\"}],\"type\":\"p\",\"id\":\"pk05n\"}],\"id\":\"p3hlz\"}],\"type\":\"tr\",\"id\":\"9zofv\"},{\"children\":[{\"type\":\"td\",\"children\":[{\"children\":[{\"text\":\"\",\"fontSize\":\"11pt\",\"color\":\"windowtext\"}],\"type\":\"p\",\"id\":\"cmlwb\"}],\"id\":\"0kfe1\"}],\"type\":\"tr\",\"id\":\"ci7uy\"}],\"type\":\"table\",\"id\":\"s33vj\"},{\"children\":[{\"text\":\"\"}],\"type\":\"p\",\"id\":\"lq0dw\"},{\"children\":[{\"children\":[{\"children\":[{\"text\":\"\"}],\"type\":\"p\",\"id\":\"4oc0v\"},{\"children\":[{\"children\":[{\"background\":\"rgb(1, 74, 150)\",\"type\":\"td\",\"attributes\":{\"rowspan\":\"2\"},\"children\":[{\"children\":[{\"text\":\"擅长领域\",\"fontSize\":\"11pt\",\"color\":\"white\"}],\"type\":\"p\",\"id\":\"3nfq1\"}],\"id\":\"yz9ii\"},{\"type\":\"td\",\"children\":[{\"children\":[{\"text\":\"围堤、防波堤设计\",\"fontSize\":\"11pt\"}],\"type\":\"p\",\"id\":\"clzls\"}],\"id\":\"m58in\"}],\"type\":\"tr\",\"id\":\"9j2yp\"},{\"children\":[{\"type\":\"td\",\"children\":[{\"children\":[{\"text\":\"航道整治设计\",\"fontSize\":\"11pt\"}],\"type\":\"p\",\"id\":\"i6zb1\"}],\"id\":\"xazfd\"}],\"type\":\"tr\",\"id\":\"ccp5c\"}],\"type\":\"table\",\"id\":\"msbdl\"}],\"type\":\"layout_child\",\"width\":\"50%\",\"id\":\"ralni\"},{\"children\":[{\"children\":[{\"text\":\"\"}],\"type\":\"p\",\"id\":\"kt4vj\"},{\"children\":[{\"children\":[{\"background\":\"rgb(1, 74, 150)\",\"type\":\"td\",\"attributes\":{\"rowspan\":\"2\"},\"children\":[{\"children\":[{\"text\":\"擅长领域\",\"fontSize\":\"11pt\",\"color\":\"white\"}],\"type\":\"p\",\"id\":\"sdi9x\"}],\"id\":\"x1j4t\"},{\"type\":\"td\",\"children\":[{\"children\":[{\"text\":\"围堤、防波堤设计\",\"fontSize\":\"11pt\"}],\"type\":\"p\",\"id\":\"s4xyn\"}],\"id\":\"17ewa\"}],\"type\":\"tr\",\"id\":\"yl9gi\"},{\"children\":[{\"type\":\"td\",\"children\":[{\"children\":[{\"text\":\"航道整治设计\",\"fontSize\":\"11pt\"}],\"type\":\"p\",\"id\":\"11uqw\"}],\"id\":\"w9gor\"}],\"type\":\"tr\",\"id\":\"1ffe4\"}],\"type\":\"table\",\"id\":\"66cl2\"}],\"type\":\"layout_child\",\"width\":\"50%\",\"id\":\"7p9pg\"}],\"layout\":\"1-1\",\"type\":\"layout\",\"id\":\"yywon\"},{\"children\":[{\"text\":\"\"}],\"type\":\"p\",\"id\":\"v1m12\"}]","kb_id":1178,"cate_id":75110,"tag_ids":""}' \
  --compressed
"""


post_method,post_url,post_headers,post_data = parse_curl(
    post_curl,['content-type','origin','user-agent']
    );

put_method,put_url,put_headers,put_data = parse_curl(
    put_curl,[]
    );

def post_rich_content_knowledge_detail(cate_parent_id:int):
    response = requests.post(
        url=post_url,
        headers=post_headers,
        timeout=5,
        data={
            'kb_id': str(kb_id),
            'cate_parent_id': str(cate_parent_id),
            'dir_type': '0'
        })
    if response.status_code == 200 or response.status_code == 201:
        response_data = response.json()
        if response_data['status'] == 200:
            return response_data['data']['cate_id'], response_data['data'][
                'knowledge_id']
        else:
            print(f"Error: {response_data}")
            return None, None
    else:
        print(f"HTTP Error: {response.status_code}")
        return None, None


def put_rich_content_knowledge_detail(knowledge_id, name, content,
                                      cate_id):
    data = {
            "knowledge_id": int(knowledge_id),
            "name": name,
            "author": 'admin',
            "content": content,
            "kb_id": int(kb_id),
            "cate_id": int(cate_id),
            "tag_ids": ""
        }
    response = requests.put(
        url=put_url,
        headers=put_headers,
        timeout=10,
        json=data)
    if response.status_code == 200 or response.status_code == 201:
        response_data = response.json()
        if response_data['status'] == 200:
            print(f"Success: {name}")
            return True
        else:
            print(f"Error: {name}")
            print(f"Error: {response_data}")
            return False
    else:
        print(f"HTTP Error: {name}")
        print(f"HTTP Error: {response.status_code}")
        return False
